package com.chen.cookie;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <b>CookieUtil</b>
 * <p>
 * 描述:<br>
 * 此工具类方便cookie的生成<br>
 * cookie的获取<br>
 * cookie封装成map集合方便读取<br>
 * 经过编码，解码
 * @author 威 
 * <br>2017年6月13日 下午11:05:34
 * @since 1.0
 */
public class CookieUtil {
	/**
	 * 该类存在的方法和方法具体描述：
	 * 
	 * addCookie		生成一个cookie
	 * 
	 * getCookieByName 	通过Name找到cookie并返回字符串的形式
	 * 
	 * doCookieToMap 	将cookie封装成map集合的形式
	 * 
	 * delelteCookie 		删除相应name值的cookie
	 * 
	 * showCookie 		读取所有cookie -- 在控制台中输出
	 */
	public static boolean addCookie(String cookie_name,
			String cookie_value, 
			HttpServletResponse response){
		return addCookie(cookie_name, cookie_value, response, 7*24*60*60);
	}
	/**
	 * 产生新的cookie
	 * <p>	 
	 * 默认有效时间7*24*60*60
	 * @param cookie_name
	 * @param cookie_value
	 * @param response
	 * @return
	 * boolean
	 * @since 1.0
	 */
	 
	public static boolean addCookie(String cookie_name,
			String cookie_value, 
			HttpServletResponse response,
			int time){
		Cookie cookie;
		try {
			cookie = new Cookie(URLEncoder.encode(cookie_name, "UTF-8"), 
					URLEncoder.encode(cookie_value, "UTF-8"));
			cookie.setMaxAge(time);
			//应用共享即服务器内共享
			cookie.setPath("/");
			//setDomain(域名)方法设计跨域共享
			//将cookie添加到response中使之生效
			response.addCookie(cookie);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return true;
	}
	/**
	 * 
	 * 返回查找的cookie值 
	 * @see
	 * @param cookie_name
	 * @param request
	 * @return
	 * String
	 *
	 */
	public static String getCookieByName(String cookie_name, javax.servlet.http.HttpServletRequest request){
		Cookie[] cookies = request.getCookies();
		if(cookies != null){
			try {
				for(Cookie cookie : cookies)
					if (URLDecoder.decode(cookie.getName(), "UTF-8").equals(cookie_name)) 
						return URLDecoder.decode(cookie.getValue(), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	/**
	 * 
	 * 将cookie对象封装成map对象返回 -- 方便了cookie的读取
	 * @see
	 * @param request
	 * @return
	 * Map<String,String>
	 *
	 */
	public static Map<String, String> doCookieToMap(javax.servlet.http.HttpServletRequest request){
		Map<String, String> map = new HashMap<String, String>();
		Cookie[] cookies = request.getCookies();
		if(cookies != null){
			try{
				for(Cookie cookie : cookies)
					map.put(URLDecoder.decode(cookie.getName(), "UTF-8"),
							URLDecoder.decode(cookie.getValue(), "UTF-8"));
				return map;
			}catch(UnsupportedEncodingException e){
				e.printStackTrace();
			}
		}
		return null;
	}
	/**
	 * 删除响应的cookie
	 * @see
	 * @param cookie_name
	 * @param request
	 * @param response
	 * @return
	 * boolean
	 */
	public static boolean delelteCookie(String cookie_name,
			HttpServletRequest request,
			HttpServletResponse response){
		Cookie[] cookies = request.getCookies();
		if(cookies != null){
			try{
				for(Cookie cookie : cookies){
					if(URLDecoder.decode(cookie.getName(), "UTF-8").equals(cookie_name)){
						cookie.setValue(null);
						cookie.setMaxAge(0);
						cookie.setPath("/");
						response.addCookie(cookie);
						break;
					}
				}
				return true;
			}catch(UnsupportedEncodingException e){
				e.printStackTrace();
			}
		}
		return false;
	}
	/**
	 * 在控制台中输出所有cookie 
	 * @see
	 * @param request
	 * void
	 */
	public static void showCookie(javax.servlet.http.HttpServletRequest request){
		Cookie[] cookies = request.getCookies();
		if(cookies!= null) 
			for(Cookie cookie : cookies)
				System.out.println(cookie.toString());		
	}
}
